package com.project.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import gz.RegistrationService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(value = {"*"},allowCredentials = "true")
public class RegistrationController {

    @Reference
    private RegistrationService rs;


    /**
     * 查询出挂号列表中所有的患者
     * @param name
     * @param registration_status
     * @param startdate
     * @param enddate
     * @return
     */
    @RequestMapping("dzm_his_Registration")
    public List<Map<String,Object>>  all(String name,Integer registration_status,String startdate,String enddate){
        List<Map<String,Object>> all = rs.find_Registration(name,registration_status,startdate,enddate);
        System.out.println("日期："+startdate+"日期："+enddate);
        boolean g = all.isEmpty();
        System.out.println("是否为空："+g);
        return all;
    }

    /**
     * 点击挂号收费按钮获取相关信息
     * @param request
     * @return
     */
    @RequestMapping("ghlb/Registration")
    public List<Map<String,Object>>  alls(HttpServletRequest request){
        String id = request.getParameter("registration_id");
        Integer ids = Integer.valueOf(id);
        System.out.println("去付款返回的参数:"+ids);
        List<Map<String,Object>> all = rs.find_Registrations(ids);
        boolean g = all.isEmpty();
        System.out.println("是否为空："+g);
        return all;
    }


    /**
     *点击作废按钮修改患者的挂号状态为已作废
     * @param request
     * @return
     */
    @RequestMapping("updateRegistration")
    public Map<String,Object> updateStatus(HttpServletRequest request){
        String id = request.getParameter("registration_id");
        Integer ids = Integer.valueOf(id);
        System.out.println("返回的参数:"+ids);
        int i = rs.updateStatus(ids);
        System.out.println(i+"行受影响");
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return map;
    }

    /**
     *点击退款按钮查询出该患者的退款信息
     * @param request
     * @return
     */
    @RequestMapping("findRegistration")
    public List<Map<String,Object>> tuikuan(HttpServletRequest request){
        String id = request.getParameter("registration_id");
        Integer ids = Integer.valueOf(id);
        System.out.println("返回的参数:"+ids);
        List<Map<String,Object>> all = rs.Money_Back(ids);
        System.out.println("查询的数据："+all);
        return all;
    }


    /**
     *点击确认退款按钮修改患者的挂号状态并添加退款信息到退款记录表中
     * @param request
     * @return
     */
    @RequestMapping("updateTow")
    public  Map<String,Object> tuikuan1(HttpServletRequest request){
        String id = request.getParameter("patient_id");
        String money = request.getParameter("money");
        Integer ids = Integer.valueOf(id);
        Double d = Double.parseDouble(money);
        System.out.println("返回的参数:"+ids);
        System.out.println("返回的金额："+d);
        int p = rs.findPckById(ids);
        int o = rs.findOrderById(ids);
        System.out.println("pckid:"+p+"*****orderid:"+o);
        int i = rs.affirm_refund(ids);
        int i2 = rs.insert_refund(d,p,o);
        System.out.println(i+i2+"行受影响");
        Map<String,Object> map = new HashMap<>();
        if(i>0&&i2>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return map;
    }


    /**
     * 点击挂号收费按钮修改患者挂号状态并添加收费信息到收费记录表中
     * @param request
     * @return
     */
    @RequestMapping("pays/gopay")
    public Map<String,Object> pay(HttpServletRequest request){
        String id = request.getParameter("registration_id");
        String p = request.getParameter("patient_id");
        String o = request.getParameter("order_id");
        String m = request.getParameter("money");
        String code = request.getParameter("case_code");
        Integer ids = Integer.valueOf(id);
        Integer patient_id = Integer.valueOf(p);
        Integer order_id = Integer.valueOf(o);
        Double money = Double.parseDouble(m);
        System.out.println("支付返回的参数:"+ids);
        int i =  rs.paymoney(ids);
        int s = rs.insert_pkg(ids,patient_id,order_id,money,code);
        int k = rs.select_pkgId(code);
        System.out.println("pkg_id:"+k);
        int ss = rs.insert_paylog(k,money);
        Map<String,Object> map = new HashMap<>();
        if(i>0&&s>0&&ss>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return map;
    }
}
